#!/bin/bash
SRC="/usr/local/include/libnetframework/AppTemplate"

DAY=`date +"%Y-%m-%d"`
USER=`users|awk '{print $1}'`

function usage()
{
	echo
	echo "Usage: enetlib -[s|m|p] ClassName [-main]"
	echo -e "\t-s : Generate a SINGLE thread server class"
	echo -e "\t-m : Generate a MULTIPLE thread server class"
	echo -e "\t-p : Use ProtocolFamily class, generate protocol_family class if not exist"
	echo -e "\t-main : Generate a Main-cpp file for the ServerClass"
	echo 
}

function generate_framework_Class()
{
	TClassName=$1   ###模板类名
	ClassName=$2    ###目标类名
	PF=$3           ###协议族
	
	MACRO=`echo $ClassName|awk '{print toupper($1)}'`   ###宏名称

	if [ -f "${ClassName}.h" ] || [ -f "${ClassName}.cpp" ];then
		echo "${ClassName}.h or ${ClassName}.cpp already exist. Do Nothing."
		return
	fi

	echo "Generate Framework Class: ${ClassName}.h, ${ClassName}.cpp..."
	### h file
	sed -e "s/\#CreateDate\#/$DAY/" -e "s/\#Author\#/$USER/" -e "s/_${TClassName}_H_/_${MACRO}_H_/" -e "s/${TClassName}/${ClassName}/g" ${SRC}/${TClassName}.h >${ClassName}.h
	### cpp file
	sed -e "s/\#CreateDate\#/$DAY/" -e "s/\#Author\#/$USER/" -e "s/${TClassName}/${ClassName}/g" ${SRC}/${TClassName}.cpp >${ClassName}.cpp

	if [ "$PF" != "" ];then
		sed -i "s/TemplateProtocolFamily/$PF/" ${ClassName}.cpp
	fi
}

function generate_protocl_family()
{
	PF=$1  ###协议族名称
	MACRO=`echo $PF|awk '{print toupper($1)}'`   ###宏名称

	if [ -f "$PF.h" ] || [ -f "$PF.cpp" ];then
		echo "$PF.h or $PF.cpp already exist. Do Nothing."
		return
	fi

	echo "Generate ProtocolFamily class: $PF.h, $PF.cpp..."
	### h file
	sed -e "s/\#CreateDate\#/$DAY/" -e "s/\#Author\#/$USER/" -e "s/_TemplateProtocolFamily_H_/_${MACRO}_H_/" -e "s/TemplateProtocolFamily/$PF/" ${SRC}/TemplateProtocolFamily.h >${PF}.h
	### cpp file
	sed -e "s/\#CreateDate\#/$DAY/" -e "s/\#Author\#/$USER/" -e "s/TemplateProtocolFamily/$PF/" ${SRC}/TemplateProtocolFamily.cpp >${PF}.cpp
}

function generate_main()
{
	ClassName=$1
	MAINCPP="${ClassName}Main.cpp"

	if [ -f "${MAINCPP}" ];then
		echo "${MAINCPP} already exist. Do Nothing."
	else
		echo "Generate Main cpp file: ${MAINCPP}"
		sed -e "s/\#CreateDate\#/$DAY/" -e "s/\#Author\#/$USER/" -e "s/AppTemplate/${ClassName}/" ${SRC}/AppTemplateMain.cpp >${MAINCPP}
	fi
}

##########################
if [ $# -lt 1 ];then
	usage;
	exit
fi

MODE=""       ### 线程模式
CLASS_NAME="" ### 类名
PRO_FAMILY="" ### 协议族类名
MAIN_FLAG=0   ### 是否生成main文件

argc=$#
i=1
while [ $i -le $argc ] ;do
	#echo "${!i}"
	if [ "${!i}" = "-s" ] || [ "${!i}" = "-m" ];then
		MODE=${!i}
		i=$((i+1))
		CLASS_NAME=${!i}
		if [ "${CLASS_NAME}" = "" ]  || [ "${CLASS_NAME:0:1}" = "-" ];then
			usage;
			exit
		fi
	elif [ "${!i}" = "-p" ];then
		i=$((i+1))
		PRO_FAMILY=${!i}
		#echo ${PRO_FAMILY}
		if [ "${PRO_FAMILY}" = "" ]  || [ "${PRO_FAMILY:0:1}" = "-" ];then
			usage;
			exit
		fi
	elif [ "${!i}" = "-main" ];then
		MAIN_FLAG=1
	fi

	i=$((i+1))
done	

#### 生成协议文件
if [ "${PRO_FAMILY}" != "" ];then
	 generate_protocl_family ${PRO_FAMILY}
fi

if [ "${MODE}" != "" ];then
	TEMPLATE="STAppTemplate"
	if [ "${MODE}" = "-m" ];then
		TEMPLATE="MTAppTemplate"
	fi
	generate_framework_Class $TEMPLATE ${CLASS_NAME} ${PRO_FAMILY}

	if [ ${MAIN_FLAG} -eq 1 ];then
		generate_main ${CLASS_NAME}
	fi	
fi

echo "finished..."
echo
